package com.xjh.basestudy.designmode.createMode.createMode2_Prototype.p3_DeepClone;

import java.util.Random;

/**
 * @Author XuJianHua
 * @Date 2022/9/24 22:29
 * @Description
 */
public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        // 浅克隆
        EnemyPlaneShallow enemyPlaneShallow = new EnemyPlaneShallow(new Random().nextInt(200));
        enemyPlaneShallow.setBulletShallow(new BulletShallow("30毫米子弹"));
        for (int i = 0; i < 3; i++) {
            // 创建克隆对象
            EnemyPlaneShallow clone = enemyPlaneShallow.clone();
            // 不同的飞机有不同的hashcode，说明飞机克隆成功
            // 不同的飞机有相同的hashcode，说明子弹克隆失败
            System.out.println("浅克隆飞机编号：" + i + "。飞机hashcode:" + clone.hashCode() + "。子弹hashcode:" + clone.getBulletShallow().hashCode());
        }
        // 深克隆
        EnemyPlaneDeep enemyPlaneDeep = new EnemyPlaneDeep(new Random().nextInt(200), new BulletDeep("100毫米子弹"));
        for (int i = 0; i < 3; i++) {
            EnemyPlaneDeep clone = enemyPlaneDeep.clone();
            // 子弹的hashcode不同，说明克隆成功。
            System.out.println("深克隆飞机编号：" + i + "。飞机hashcode:" + clone.hashCode() + "。子弹hashcode:" + clone.getBulletDeep().hashCode());
        }
    }
}
